var errHideTimeOut;
var tooltipTop = 28;

(function($){
	$.getStringLength=function(str)
	{
		str = $.trim(str);
		
		if(str=="")
			return 0; 
			
		var length=0; 
		for(var i=0;i <str.length;i++) 
		{ 
			if(str.charCodeAt(i)>255)
				length+=2; 
			else
				length++; 
		}
		
		return length;
	}
	
	$.getLengthString=function(str,length,isSpace)
	{
		if(arguments.length < 3)
			var isSpace = true; 
		
		if($.trim(str)=="")
			return "";
		
		var tempStr="";
		var strLength = 0;
		
		for(var i=0;i <str.length;i++) 
		{
			if(str.charCodeAt(i)>255)
				strLength+=2;
			else
			{
				if(str.charAt(i) == " ")
				{
					if(	isSpace)
						strLength++;	
				}
				else
					strLength++;
			}
				
			if(length >= strLength)
				tempStr += str.charAt(i);
		}
		
		return tempStr;
	}
	
	$.copyText = function(id)
	{
		var txt = $(id).val();
		if(window.clipboardData)
		{
			window.clipboardData.clearData();
			var judge = window.clipboardData.setData("Text", txt);
			if(judge === true)
				alert("复制成功！");
			else
				alert("不允许复制，请您手动进行");
		}
		else if(navigator.userAgent.indexOf("Opera") != -1)
		{
			window.location = txt;
		} 
		else if (window.netscape) 
		{
			try
			{
				netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
			}
			catch(e)
			{
				alert("被浏览器拒绝！\n请在浏览器地址栏输入'about:config'并回车\n然后将'signed.applets.codebase_principal_support'设置为'true'");
			}
			var clip = Components.classes['@mozilla.org/widget/clipboard;1'].createInstance(Components.interfaces.nsIClipboard);
			if (!clip)
				return;
			var trans = Components.classes['@mozilla.org/widget/transferable;1'].createInstance(Components.interfaces.nsITransferable);
			if (!trans)
				return;
			trans.addDataFlavor('text/unicode');
			var str = new Object();
			var len = new Object();
			var str = Components.classes["@mozilla.org/supports-string;1"].createInstance(Components.interfaces.nsISupportsString);
			var copytext = txt;
			str.data = copytext;
			trans.setTransferData("text/unicode",str,copytext.length*2);
			var clipid = Components.interfaces.nsIClipboard;
			if (!clip)
				return false;
			clip.setData(trans,null,clipid.kGlobalClipboard);
			alert("复制成功！");
		}
	};
	
	$(window).scroll(function(){
		if($("#sysmsg-error") != "none" || $("#sysmsg-success") != "none")
		{
			var top = $.getBodyScrollTop();
			if(top < tooltipTop)
				top = tooltipTop;
			$("#sysmsg-box").stop();
			$("#sysmsg-box").animate({"top":top},{duration:300}); 
		}
	});
	
	$.showErr = function(str)
	{
		var top = $.getBodyScrollTop();
		
		if(top < tooltipTop)
			top = tooltipTop;
		
		$("#sysmsg-box").css({"top":top});
		$("#sysmsg-error span:first").html(str);
		$("#sysmsg-error").show();
		$("#sysmsg-success").hide();
		$("#sysmsg-box").show();
		
		clearTimeout(errHideTimeOut);
		
		var hideErr = function(){
			$("#sysmsg-box").slideUp(300);
		};
		
		errHideTimeOut = setTimeout(hideErr,5000);
		
		$("#sysmsg-box .close").one("click", function(){
			$("#sysmsg-box").hide();
		});
	}
	
	$.showSuccess = function(str)
	{
		var top = $.getBodyScrollTop();
		if(top < tooltipTop)
			top = tooltipTop;
		$("#sysmsg-box").css({"top":top});
		$("#sysmsg-success span:first").html(str);
		$("#sysmsg-success").show();
		$("#sysmsg-error").hide();
		$("#sysmsg-box").show();
		
		clearTimeout(errHideTimeOut);
		
		var hideErr = function(){
			$("#sysmsg-box").slideUp(300);
		};
		
		errHideTimeOut = setTimeout(hideErr,5000);
		
		$("#sysmsg-box .close").one("click", function(){
			$("#sysmsg-box").hide();
		});
	}
	
	$.showWindow=function(option)
	{
		option = $.extend({
			title:null,
			msg:null,
			fwindow:'#fwindow',
			html:null,
			innerObj:null,
			closeFun:null
		}, option || {});
		
		var selfObj=$(option.fwindow);
		
		if(option.title)
			$('.fwindow_top h3',selfObj).html(option.title);
		
		if(option.msg)
			$('.fwindow_msg',selfObj).show().html(option.msg);
		else
			$('.fwindow_msg',selfObj).hide();
			
		if(option.html)
			$('.fwindow_content',selfObj).html(option.html);
			
		if(option.innerObj)
		{
			$('.fwindow_content',selfObj).append(option.innerObj);
			option.innerObj.show();
		}
		
		var windowWH = $.getWindowWH();
		$("#fscreen").css({"width":windowWH[0],"height":windowWH[1]});
		$("#fscreen").bgiframe();
		$("#fscreen").show();
        
		selfObj.css({"left":(windowWH[0] - selfObj.width()) / 2,"top":($(window).height() - selfObj.height()) / 2});
		selfObj.bgiframe();
        selfObj.show();

		$(".close",selfObj).click(function(){
			$("#fscreen").hide();
            selfObj.hide();
			$('.fwindow_top h3',selfObj).html('');
			$('.fwindow_msg',selfObj).html('');
			
			if(option.html)
				$('.fwindow_content',selfObj).html('');
			
			if(option.closeFun)
               option.closeFun.call(this);
        });
	}
	
	$.windowCenter=function(obj)
	{
		var windowWH = $.getWindowWH();
		var windowWidth=windowWH[0];
		var windowHeight=windowWH[1];
		var objWidth=obj.width();
		var objHeight=obj.height();
		var objTop=tooltipTop + $.getBodyScrollTop();
		var objLeft=(windowWidth - objWidth ) / 2;
		obj.css({position:"absolute",display:"block","z-index":1000,top:objTop,left:objLeft});
	}
	
	$.getBodyScrollTop=function(){
        var scrollPos; 
        if (typeof window.pageYOffset != 'undefined') { 
            scrollPos = window.pageYOffset; 
        } 
        else if (typeof document.compatMode != 'undefined' && 
            document.compatMode != 'BackCompat') { 
            scrollPos = document.documentElement.scrollTop; 
        } 
        else if (typeof document.body != 'undefined') { 
            scrollPos = document.body.scrollTop; 
        } 
        return scrollPos;
    }
	
	$.getWindowWH = function(){
		var width=$.support.cssFloat ? $(document.documentElement).width() : $(window).width();
		var height=$.support.cssFloat ? $(document.documentElement).height() : $(document).height();
		return [width,height];
	}
	
	$.showLoading = function(obj){
		obj = $(obj);
		obj.append($("#floading").html());
		$(".floading",obj).css({"z-index":1000,"width":obj.width(),"height":obj.height()});
		$(".floading",obj).show().bgiframe();
	};
	
	$.closeLoading = function(obj){
		$('.floading',$(obj)).remove();
	};
	
	$.checkRequire = function(value){
		var reg = /.+/;
        return reg.test($.trim(value));
	}
	
	$.minLength = function(value, length , isByte) {
		var strLength = $.trim(value).length;
		if(isByte)
			strLength = $.getStringLength(value);
			
		return strLength >= length;
	};
	
	$.maxLength = function(value, length , isByte) {
		var strLength = $.trim(value).length;
		if(isByte)
			strLength = $.getStringLength(value);
			
		return strLength <= length;
	};
	
	$.rangeLength = function(value, minLength,maxLength, isByte) {
		var strLength = $.trim(value).length;
		if(isByte)
			strLength = $.getStringLength(value);
			
		return strLength >= minLength && strLength <= maxLength;
	}
	
	$.checkMobilePhone = function(value){
		return /^(13\d{9}|18\d{9}|15\d{9})$/i.test($.trim(value));
	}
	
	$.checkPhone = function(val){
  		var flag = 0;
		val = $.trim(val);
  		var num = ".0123456789/-()";
  		for(var i = 0; i < (val.length); i++)
		{
    		tmp = val.substring(i, i + 1);
    		if(num.indexOf(tmp) < 0)
      			flag++;
 		}
  		if(flag > 0)
			return true;
		else
			return false;
	}
	
	$.checkEmail = function(val){
		var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/; 
		return reg.test(val);
	};
	
	$.checkUrl = function(val){
		var reg = /^http:\/\/[A-Za-z0-9]+\.[A-Za-z0-9]+[\/=\?%\-&_~`@[\]\':+!]*([^<>\"\"])*$/; 
        return reg.test(val);
	};
	
	$.checkCurrency = function(val){
		var reg = /^\d+(\.\d+)?$/; 
        return reg.test(val);
	};
	
	$.checkNumber = function(val){
		var reg = /^\d+$/; 
        return reg.test(val);
	};
	
	$.checkInteger = function(val){
		var reg = /^[-\+]?\d+$/; 
        return reg.test(val);
	};
	
	$.checkDouble = function(val){
		var reg = /^[-\+]?\d+(\.\d+)?$/; 
        return reg.test(val);
	};
	
	$.checkEnglish = function(val){
		var reg = /^[A-Za-z]+$/; 
        return reg.test(val);
	};
	
	$.checkQQMsn = function(val){
		var reg = /^[1-9]*[1-9][0-9]*$|^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/; 
        return reg.test(val);
	};
	
})(jQuery);

jQuery(function($){
	$(".search-input").focus(function(){
		if($.trim(this.value) == this.getAttribute("empty"))
			this.value = "";
		$(this).removeClass("inactive");
			
	}).blur(function(){
		if($.trim(this.value) == "" || $.trim(this.value) == this.getAttribute("empty"))
		{
			this.value = this.getAttribute("empty");
			$(this).addClass("inactive");
		}
	});
	
	if($("#sysmsg-error") != "none" || $("#sysmsg-success") != "none")
	{
		var hideErr = function(){
			$("#sysmsg-box").slideUp(300);
		};
		
		errHideTimeOut = setTimeout(hideErr,5000);
		
		$("#sysmsg-box .close").one("click", function(){
			$("#sysmsg-box").hide();
		});
	}
});

function homePage(obj,val)
{   
	try
	{
		obj.style.behavior='url(#default#homepage)';
		obj.setHomePage(val);
	}   
	catch(e)
	{
		if(window.netscape)
		{
			try
			{   
				 netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");     
			}
			catch (e)
			{
				alert("此操作被浏览器拒绝！");
			}
			
			var prefs = Components.classes['@mozilla.org/preferences-service;1'].getService(Components.interfaces.nsIPrefBranch);
			prefs.setCharPref('browser.startup.homepage',val);
		} 
	}
}
